 
package top.ewind.im.chat.util;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


import top.ewind.im.nio.mutual.Message;


 
public class MessageDispatcher  {
    private static BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
	private static ThreadPoolExecutor executor =  new ThreadPoolExecutor(3, 5, 20,	TimeUnit.SECONDS,queue);;
	final static String sendUrl = "http://%1$s/ichat/message";
    
    public static void  execute(final Message msg,final String ip){
	    executor.execute(new Runnable() {
			@Override
			public void run() {
				try {
					httpPost(String.format(sendUrl,ip),msg);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		 });
	}
 
	
	private static String httpPost(String url,Message msg) throws Exception {
//		CloseableHttpClient httpclient = HttpClients.createDefault();
//		HttpPost httpPost = new HttpPost(url);
//        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
//        nvps.add(new BasicNameValuePair("mid", msg.getMid()));
//        nvps.add(new BasicNameValuePair("type", msg.getType()));
//        nvps.add(new BasicNameValuePair("title", msg.getTitle()));
//        nvps.add(new BasicNameValuePair("content",msg.getContent()));
//        nvps.add(new BasicNameValuePair("sender", msg.getSender()));
//        nvps.add(new BasicNameValuePair("receiver",msg.getReceiver()));
//        nvps.add(new BasicNameValuePair("file",msg.getFile()));
//        nvps.add(new BasicNameValuePair("fileType",msg.getFileType()));
//        nvps.add(new BasicNameValuePair("timestamp",String.valueOf(msg.getTimestamp())));
//        httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));
//        CloseableHttpResponse response2 = httpclient.execute(httpPost);
//        String data = null;
//        try {
//            HttpEntity entity2 = response2.getEntity();
//            data = EntityUtils.toString(entity2);
//        } finally {
//            response2.close();
//        }
		//TODO
        return "";
	}
	
  
}
